﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kaidou.Util;
using System.Net;
using System.Net.Mail;
using System.IO;

namespace Kaidou.Bll
{
    public class BatchMailSendManager
    {
     
        /**
	     * メールを送信します。
	     */
        public static void sendMail(string RootPath, string Subject, string Body)
        {
            try
            {
                string framework_mail_default_messageid = null;
                string framework_mail_default_messagepw = null;
                string framework_mail_default_from = null;
                string framework_mail_default_smtpserver = null;

                try
                {
                    StreamReader objReader = new StreamReader(RootPath);
                    string a = objReader.ReadLine();
                    string b = objReader.ReadLine();
                    string c = objReader.ReadLine();
                    string d = objReader.ReadLine();
                    //string e = objReader.ReadLine();
                    objReader.Close();

                    string[] a1 = a.Split('=');
                    string[] b1 = b.Split('=');
                    string[] c1 = c.Split('=');
                    string[] d1 = d.Split('=');
                    //string[] e1 = e.Split('=');

                    framework_mail_default_messageid = b1[1];
                    framework_mail_default_messagepw = c1[1];
                    framework_mail_default_from = a1[1];
                    framework_mail_default_smtpserver = d1[1];
                    //framework_mail_default_to = e1[1];



                }
                catch (Exception e2)
                {
                    framework_mail_default_messageid = "";
                    framework_mail_default_messagepw = "";
                }

                Encoding encode = Encoding.GetEncoding("utf-8");

                SmtpClient smtp = new SmtpClient(); 
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
                smtp.EnableSsl = false;
                smtp.Host = framework_mail_default_smtpserver; 
                smtp.Port = 25;            
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = new NetworkCredential(framework_mail_default_messageid, framework_mail_default_messagepw);
                MailMessage mm = new MailMessage(); 
                mm.Priority = MailPriority.High;
                mm.From = new MailAddress(framework_mail_default_from, framework_mail_default_from, encode);

                mm.To.Add(new MailAddress("yoshinaga@it-art.jp", "yoshinaga@it-art.jp", encode));
                mm.CC.Add(new MailAddress("kobayashi@supports.co.jp", "kobayashi@supports.co.jp", encode));
                mm.CC.Add(new MailAddress("terashima@supports.co.jp", "terashima@supports.co.jp", encode));
                
                
                // 件名 (Subject) をセット
                mm.Subject = Subject;// EncodeMailHeader(mail.getSubject(), encode);
                
                // 本文をセット
                mm.Body = Body;
               
                mm.SubjectEncoding = Encoding.GetEncoding("utf-8");

                mm.IsBodyHtml = false; 

                mm.BodyEncoding = encode;

                smtp.Send(mm); 

            }catch(Exception mailex){
                LogManager.error("send mail fail!!!"+mailex.Message);
            }
        }

        
    }
}
